package se.rkrantz.simpleraid.hibernate.dao.impl;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import se.rkrantz.simpleraid.hibernate.dao.WowCharacterDao;
import se.rkrantz.simpleraid.hibernate.domain.Signup;
import se.rkrantz.simpleraid.hibernate.domain.WowCharacter;

public class WowCharacterDaoImpl extends HibernateDaoSupport implements WowCharacterDao {

	@Override
	public void save(WowCharacter wowCharacter) {
		getHibernateTemplate().save(wowCharacter);
	}

	@Override
	public void update(WowCharacter wowCharacter) {
		getHibernateTemplate().update(wowCharacter);
	}

	@Override
	public void delete(WowCharacter wowCharacter) {
		getHibernateTemplate().delete(wowCharacter);
	}

	@Override
	public WowCharacter findByCharacterById(long characterId) {
		return (WowCharacter) getHibernateTemplate().get(WowCharacter.class, characterId);
	}
	
	public int numberOfPasses(String characterId) {
		Query query = getSession().createQuery("SELECT count(*) FROM Signup WHERE status=:pass");
		query.setString("pass", Signup.Status.Passed.toString());
		Long count = (Long)query.uniqueResult();
		if (count == null) {
			return 0;
		} else {
			return count.intValue();
		}
	}
}
